#! /usr/bin/python

import mathex
from copy import copy
from copy import deepcopy as dc

L = [{3:0,4:0,5:0,6:0,7:0,8:0} for k in range(10000)]          #0 -- 9999

def init(f,tp):
    k = 1
    i = 1
    while k<10000:
        L[k][tp] = 1
        i += 1
        k = f(i)
def find(status,sh):
    if sh < 10:
        return False
    if reduce(lambda x,y:x*y, status.values()):
        #all six num found
        #is cyclic?
        if status[3]/100 == sh:
            print status
            print sum(status.values())
            return True
        else:
            return False
    else:
        for key in status.keys():
            if status[key] == 0:
                for n,item in enumerate(L[sh*100:(sh+1)*100]):
                    n = sh*100 + n
                    if item[key]:
                        st = dc(status)
                        st[key] = n
                        if find(st,n%100):return True
        return False
def main():
    init(lambda x:x*(x+1)/2, 3)
    init(lambda x:x*x, 4)
    init(lambda x:x*(3*x-1)/2, 5)
    init(lambda x:x*(2*x-1), 6)
    init(lambda x:x*(5*x-3)/2, 7)
    init(lambda x:x*(3*x-2), 8)
    #
    for n,item in enumerate(L[1000:]):
        n = n+1000
        if item[3]:
            st = {3:n,4:0,5:0,6:0,7:0,8:0}
            if find(st,n%100):
                return
if __name__ == '__main__':
    main()
